package com.bit.mybatis.mapper;

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
//    @Select("select * from user_info")
//    @Select("select `id`, `username`, `password`, age, gender, phone, " +
//            " delete_flag as deleteFlag, " +
//            " create_time as createTime, " +
//            " update_time as updateTime " +
//            " from user_info ")


//    @Results(id = "BaseMap", value = {
//            @Result(column = "delete_flag", property = "deleteFlag"),
//            @Result(column = "create_time", property = "createTime"),
//            @Result(column = "update_time", property = "updateTime")
//    })
    @Select("select * from user_info")
    List<UserInfo> selectAll();

//    @ResultMap("BaseMap")
    @Select("select * from user_info where id = #{id}")
    UserInfo selectById(Integer id);

    @Select("select * from user_info where username = #{name}")
    List<UserInfo> selectByName(String name);

    @Select("select * from user_info where id = ${id}")
    UserInfo selectById2(Integer id);

    @Select("select * from user_info where username = '${name}'")
    List<UserInfo> selectByName2(String name);
    @Select("SELECT * FROM `user_info` where age = #{age} and delete_flag = #{flag}")
    List<UserInfo> selectByAge(@Param("flag") Integer deleteFlag, Integer age);

    @Insert("insert into user_info (username,password,age) values (#{username},#{password},#{age})")
    Integer insertUser(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username,password,age) values (#{userInfo.username},#{userInfo.password},#{userInfo.age})")
    Integer insertUser2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from user_info where id = #{id}")
    Integer deleteById(Integer id);

    @Update("update user_info set password = #{password} where id = #{id}")
    Integer updateById(UserInfo userInfo);

    @Select("select * from user_info ORDER BY id ${order}")
    List<UserInfo> selectByOrder(String order);

    @Select("select * from user_info where username like CONCAT('%',#{name}, '%')")
    List<UserInfo> selectByLike(String name);

    @Select("<script>" +
            "select * from user_info" +
            "<where> " +
            "<if test='age!=null'> and age = #{age} </if>" +
            "<if test='gender!=null'> and gender = #{gender} </if>"+
            "</where>" +
            "</script>")
    List<UserInfo> selectByCondition(UserInfo userInfo);

}
